<!DOCTYPE html>
<html xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>vue-boot 部门管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../static/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../static/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../static/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../static/css/style.css">
    <link rel="stylesheet" href="../static/plugins/pageination/zpageNav.css">
    <script src="../static/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../static/plugins/bootstrap/js/bootstrap.min.js"></script>
    <style>
        .fileinput-button { position: relative; display: inline-block; overflow: hidden; } .fileinput-button input{ position: absolute; left: 0px; top: 0px; opacity: 0; -ms-filter: 'alpha(opacity=0)'; }
    </style>
</head>

<body class="hold-transition skin-red sidebar-mini">

<div id="app">
    <div class="box-header with-border" style="height: 64.5px;line-height: 54px;">
        <h3 class="box-title">部门管理</h3>
    </div>
    <div class="box-body">
        <!-- 数据表格 -->
        <div class="table-box">
            <!--工具栏-->
            <div class="pull-left">
                <div class="form-group form-inline">
                    <div class="btn-group">
                        <button type="button" sec:authorize="hasRole('dptadmin')" class="btn btn-default" title="新建" @click="getAllOneLevel();clear();" data-toggle="modal"
                                data-target="#editModal"><i class="fa fa-file-o"></i> 新建
                        </button>
                        <button type="button" sec:authorize="hasRole('dptadmin')" class="btn btn-default" title="删除" @click="deleteDepartments()"><i
                                class="fa fa-trash-o"></i> 删除
                        </button>
                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i
                                class="fa fa-refresh"></i> 刷新
                        </button>


                    </div>
                    <div class="box-tools pull-right">
                        <div class="has-feedback" style="margin-left: 750px; font-size: 16px">
                            用户名查询: <input type="text" v-model="key" class="form-control">
                            <input class="btn btn-default" type="button" @click="pageHandler(1)" value="查询">
                        </div>
                    </div>
                </div>
            </div>
            <div class="box-tools pull-right">
                <div class="has-feedback"></div>
            </div>
            <!--数据列表-->
            <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
                <thead>
                <tr>
                    <th class="" style="padding-right:0px">
                        <input id="selall" type="checkbox" class="icheckbox_square-blue">
                    </th>
                    <th class="sorting_asc">部门id</th>
                    <th class="sorting_asc">部门名称</th>
                    <th class="sorting">部门负责人</th>
                    <th class="sorting">部门级别</th>
                    <th class="text-center" sec:authorize="hasRole('dptadmin')">操作</th>
                </tr>
                </thead>
                <tbody>
                <tr v-for="departmentItem in departmentList">
                    <td><input type="checkbox" @click="deleteSelect($event,departmentItem.id)"></td>
                    <td>{{departmentItem.id}}</td>
                    <td>{{departmentItem.dptName}}</td>
                    <td>{{departmentItem.dptManager}}</td>
                    <td>{{dptLevelName[departmentItem.dptLevel]}}</td>
                    <td class="text-center" sec:authorize="hasRole('dptadmin')">
                        <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#detailModal"
                                @click="findDetailById(departmentItem.id);getAllOneLevel();getAllDptUser(departmentItem.dptName)">详情信息
                        </button>
<!--                        <button type="button"  @click="changeStatus(departmentItem.id)" style="outline:none;border-radius:4px;font-size:2px;width:36px;height:22px;background-color: red;color: white;border: none">-->
<!--                        </button>-->
                    </td>
                </tr>

                </tbody>

            </table>

        </div>
    </div>
    <!--分页-->
    <zpagenav v-bind:page="page" v-bind:page-size="pageSize" v-bind:total="total"
              v-bind:max-page="maxPage" v-on:pagehandler="pageHandler">
    </zpagenav>

    <!-- 部门详情 -->
    <div class="modal fade" id="detailModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
         aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h3 >部门详情</h3>
                </div>
                <div class="modal-body">
                    <table class="table table-bordered table-striped" width="800px">
                        <tr>
                            <td>部门ID</td>
                            <td><input class="form-control" v-model="dptDetail.id" readonly="readonly"></td>
                        </tr>
                        <tr>

                            <td>部门名称</td>
                            <td><input class="form-control" v-model="dptDetail.dptName" readonly="readonly"></td>
                        </tr>
                        <tr>
                            <td>所属部门</td>
                            <td>
                                <select  class="form-control" v-model="dptDetail.belongDpt">
                                    <option v-for="OneLevelItem in OneLevelList" selected="dptDetail.belongDpt">{{OneLevelItem.dptName}}</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td>部门负责人</td>
                            <td>
                                <select  class="form-control" v-model="dptDetail.dptManager">
                                    <option v-for="departmentUserItem in departmentUserList" selected="dptDetail.dptManager">{{departmentUserItem.username}}</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td>部门人数</td>
                            <td><input class="form-control" v-model="dptDetail.dptCount" readonly="readonly"></td>
                        </tr>
                        <tr>
                            <td>部门级别</td>
                            <td><label class="form-control" v-model="dptDetail.dptLevel"  >{{dptLevelName[dptDetail.dptLevel]}}</label></td>
                        </tr>
                    </table>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" @click="saveDetail()">保存</button>
                    <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 编辑窗口 -->
    <div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
         aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h3 id="myModalLabel">新建部门</h3>
                </div>
                <div class="modal-body">
                    <table class="table table-bordered table-striped" width="800px">
                        <tr>
                            <td>部门名称</td>
                            <td><input class="form-control" v-model="newDpt.dptName"></td>
                        </tr>
                        <tr>
                            <td>所属部门</td>
                            <td>
                                <select  class="form-control" v-model="newDpt.belongDpt">
                                    <option v-for="OneLevelItem in OneLevelList" >{{OneLevelItem.dptName}}</option>
                                </select>
                            </td>
                        </tr>
                    </table>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" @click="save(dptDetail.id)">保存</button>
                    <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
                </div>
            </div>
        </div>
    </div>
</div>


</body>


<script src="../static/plugins/vue/vuejs-2.5.16.js"></script>
<script src="../static/plugins/vue/axios-0.18.0.js"></script>
<script src="../static/plugins/vue/qs.js"></script>
<script src="../static/plugins/pageination/zpageNav.js"></script>
<script>

    new Vue({
        el: "#app",
        data: {
            page: 1,//当前页
            pageSize: 5,//每页显示数据
            total: 0,//记录总数
            key: '', //查询关键字
            departmentList: [],
            selectIds:[],
            dptDetail:{},
            dptLevelName:{1:"一级部门",2:"二级部门"},
            departmentUserList:[],
            OneLevelList:[],
            newDpt:{},
        },
        methods: {
            pageHandler: function (page) {
                var _this=this;
                this.page = page;
                axios.post("/department/search?page="+page+"&size="+this.pageSize+"&key="+this.key).then(function (response) {
                    _this.departmentList=response.data.data.records;
                    _this.total=response.data.data.total;
                    _this.pageSize = response.data.data.size;
                }).catch(function (result) {
                    window.alert(result);
                });
            },

            deleteSelect: function (event, id) {
                if (event.target.checked) {
                    this.selectIds.push(id);
                } else {
                    var idx = this.selectIds.indexOf(id);
                    this.selectIds.splice(idx, 1);
                }
            },
            deleteDepartments: function () {
                var _this = this;                     //将对象行序列化成url进行传输
                axios.post("/department/delDepartments",Qs.stringify({ids:_this.selectIds},{indices:false})).then(function (response) {
                    if (response.data.code == 200) {
                        _this.pageHandler(_this.page);
                        window.alert(response.data.msg);
                    } else {
                        window.alert(response.data.msg);
                    }
                }).catch(function (result) {
                    window.alert(result);

                });
            },
            findDetailById: function (id) {
                var _this = this;
                axios.post("/department/findDetailById?id=" + id).then(function (response) {
                    _this.dptDetail = response.data.data.Department;
                }).catch(function (result) {
                    console.log(result);
                });
            },
            //获得当前部门的所有人员
            getAllDptUser: function (dptName) {
                var _this = this;
                axios.post("/departmentUser/getAllDptUser?dptName="+dptName).then(function (response) {
                _this.departmentUserList = response.data.data.list;
              }).catch(function (result) {
                 console.log(result);
                });
             },
            save:function () {
                var _this = this;
                axios.post("/department/save",_this.newDpt).then(function (response) {
                    if (response.data.code == 200) {
                        _this.pageHandler(_this.page);
                        window.alert(response.data.msg);
                    } else {
                        window.alert(response.data.msg);
                    }
                }).catch(function (result) {
                    console.log(result);
                });
            },
            getAllOneLevel:function () {
                var _this = this;
                axios.post("/department/getAllOneLevel").then(function (response) {
                    _this.OneLevelList = response.data.data.AllOneLevel;
                }).catch(function (result) {
                    console.log(result);
                });
            },
            clear: function () {
                this.newDpt = {};
            },
            saveDetail: function () {
                var _this = this;
                axios.post("/department/saveDetail",this.dptDetail).then(function (response) {
                    if (response.data.code == 200) {
                        _this.pageHandler(_this.page);
                        window.alert(response.data.msg);
                    } else {
                        window.alert(response.data.msg);
                    }
                }).catch(function (result) {
                    console.log(result);
                });

            }
        },
        created: function () {
            this.pageHandler(1);
        }
    })

</script>


</html>